package com.example.qiaoruian.shequ.fragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;

import com.example.qiaoruian.shequ.R;
import com.example.qiaoruian.shequ.adapter.GridViewAdapter;
import com.example.qiaoruian.shequ.bean.User;
import com.example.qiaoruian.shequ.view.BaseFragment;
import com.example.qiaoruian.shequ.view.MyTitle;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qiaoruian on 2017/5/8.
 */

public class ServcesFragment extends BaseFragment {

    private MyTitle mTitle;
    private GridView gv;
    private int[] res = {R.drawable.fangwuzhongjie_03, R.drawable.banjia02_03, R.drawable.zhuangxiu04_03, R.drawable.dianqi03_03, R.drawable.qiche05_03, R.drawable.wangluo06_03, R.drawable.songcan07_03, R.drawable.kuaidi08_03, R.drawable.jiazheng09_03};
    private String[] str = {"房屋中介","搬家服务", "装修服务", "电器服务", "汽车服务", "网络服务", "送餐服务", "快递服务", "家政服务"};
        private   List<User> list =null;
    private LinearLayout gridViewLayout;

    @Override
    protected void initData() {

    }

    @Override
    protected void initViewData(View v) {
        mTitle = (MyTitle) v.findViewById(R.id.title);
        gridViewLayout = (LinearLayout) v.findViewById(R.id.gridViewLayout);
        mTitle.getTv_title().setText("居家服务");
        mTitle.getIv_item().setVisibility(View.VISIBLE);
        mTitle.getIv_item2().setVisibility(View.GONE);
        list = getData();
        gv = (GridView) v.findViewById(R.id.gv);
        GridViewAdapter gridViewAdapter= new GridViewAdapter(list, getActivity(),gridViewLayout);
        gv.setAdapter(gridViewAdapter);


    }
    public static void setListViewHeightBasedOnChildren(GridView listView) {
        // 获取listview的adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        // 固定列宽，有多少列
        int col = 3;// listView.getNumColumns();
        int totalHeight = 0;
        // i每次加4，相当于listAdapter.getCount()小于等于4时 循环一次，计算一次item的高度，
        // listAdapter.getCount()小于等于8时计算两次高度相加
        for (int i = 0; i < listAdapter.getCount(); i += col) {
            // 获取listview的每一个item
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            // 获取item的高度和
            totalHeight += listItem.getMeasuredHeight();
        }

        // 获取listview的布局参数
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        // 设置高度
        params.height = totalHeight;
        // 设置margin
        ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10);
        // 设置参数
        listView.setLayoutParams(params);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.service_fragment;
    }

    public List<User> getData() {
        List<User> l = new ArrayList<>();
        User u = null;
        for (int i = 0; i < res.length; i++) {
            u = new User(res[i],str[i]);
            l.add(u);
        }
        return l;
    }
}
